import os
import time

import openpyxl

from commen.log import Logger

time_now = time.strftime("%Y%m%d-%H%M", time.localtime())
# logger = Logger(logger='TestMylog').getlog()
print("欢迎使用本工具")
print("本工具使用时需要将log文件放入工具所在当前目录，输出的结果也会在当前目录")
print("结果文件按照时间分割，最小单位1分钟，如果1分钟内操作，结果文件会被覆盖")
while True:
    option = input("请输入选项值，1.为0001测试，2.为00FF测试：")
    try:
        with open("./mcucomm.log", encoding="UTF-8") as f:
            lines = f.readlines()
    except:
        print("当前目录未找到log文件")
        print("请在当前目录放置mcucomm.log文件【文件名不可改变】，再次选择测试脚本")
        continue
    main_key = "83 07"
    book = openpyxl.Workbook()
    sheet = book.create_sheet(index=0)
    if option == "1":
        print("0001测试脚本开始执行")
        save_path = os.getcwd() + os.sep + f"{time_now}_0001测试结果.xlsx"
        sheet.cell(1, 2).value = "mcu系统时间"
        sheet.cell(1, 3).value = "mcu错误数据"
        sheet.cell(1, 4).value = "错误原因"
        row = 1
        for i in lines:
            if main_key in i:
                try:
                    main_info = i.split("+40:")[1].replace(" ", "")[2:6]
                except:
                    continue
                if main_info == "0001":
                    # logger.info("成功")
                    continue
                else:
                    row += 1
                    error_time = i.split("<thread")[0].rstrip()
                    sheet.cell(row, 2).value = error_time
                    sheet.cell(row, 3).value = main_info
                    if 'debug' in i:
                        sheet.cell(row, 4).value = "数据错误"
                    else:
                        sheet.cell(row, 4).value = "warning级别日志"
                    # logger.error(f"异常数据{error_time}")
                    print((f"异常数据{error_time}"))
        book.save(save_path)

    elif option == "2":
        print("00FF测试脚本开始执行")
        save_path = os.getcwd() + os.sep + f"{time_now}_00FF测试结果.xlsx"
        sheet.cell(1, 2).value = "mcu系统时间"
        sheet.cell(1, 3).value = "mcu错误数据"
        sheet.cell(1, 4).value = "错误原因"
        row = 1
        assert_number = 1
        for i in lines:
            if main_key in i:
                try:
                    main_info = i.split("+40:")[1].replace(" ", "")[2:6]
                except:
                    continue
                try:
                    real_number = int(main_info, 16)
                except:
                    row += 1
                    error_time = i.split("<thread")[0].rstrip()
                    sheet.cell(row, 2).value = error_time
                    sheet.cell(row, 3).value = main_info
                    sheet.cell(row, 4).value = "数据内容错误"
                    continue

                if int(main_info, 16) == assert_number:
                    continue
                else:
                    row += 1
                    error_time = i.split("<thread")[0].rstrip()
                    sheet.cell(row, 2).value = error_time
                    sheet.cell(row, 3).value = main_info
                    if 'debug' in i:
                        sheet.cell(row, 4).value = "数据错误"
                    else:
                        sheet.cell(row, 4).value = "warning级别日志"
                    # logger.error(f"异常数据{error_time}")
                    print((f"异常数据{error_time}"))
                if assert_number < 255:
                    assert_number += 1
        book.save(save_path)
    print("本轮测试完成，输入选项继续下一轮测试....")